package com.dh.leetcode.five;

import org.junit.Assert;
import org.junit.Test;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: _500_keyboard_row
 * @Description: 500. 键盘行
 * 给你一个字符串数组 words ，只返回可以使用在 美式键盘 同一行的字母打印出来的单词。键盘如下图所示。
 * <p>
 * 美式键盘 中：
 * <p>
 * 第一行由字符 "qwertyuiop" 组成。
 * 第二行由字符 "asdfghjkl" 组成。
 * 第三行由字符 "zxcvbnm" 组成。
 * American keyboard
 * <p>
 * <p>
 * <p>
 * 示例 1：
 * <p>
 * 输入：words = ["Hello","Alaska","Dad","Peace"]
 * 输出：["Alaska","Dad"]
 * 示例 2：
 * <p>
 * 输入：words = ["omk"]
 * 输出：[]
 * 示例 3：
 * <p>
 * 输入：words = ["adsdf","sfd"]
 * 输出：["adsdf","sfd"]
 * <p>
 * https://leetcode-cn.com/problems/keyboard-row/
 * @Author: shouzimu
 * @Date: 2021/2/3 16:51
 */
public class _500_keyboard_row {
    //第一版三个set代码冗余
    /*
    static Set<Character> line1Set = new HashSet<>(10);
    static Set<Character> line2Set = new HashSet<>(10);
    static Set<Character> line3Set = new HashSet<>(10);

    static {
        for (char c : "qwertyuiop".toCharArray()) {
            line1Set.add(c);
        }
        for (char c : "asdfghjkl".toCharArray()) {
            line2Set.add(c);
        }
        for (char c : "zxcvbnm".toCharArray()) {
            line3Set.add(c);
        }
    }

    public String[] findWords2(String[] words) {
        List<String> res = new ArrayList<>();
        for (String word : words) {
            char[] chars = word.toCharArray();
            boolean lineOne = true;
            Set set = null;
            for (char aChar : chars) {
                char c = Character.toLowerCase(aChar);
                if (null == set) {
                    if (line1Set.contains(c)) {
                        set = line1Set;
                    } else if (line2Set.contains(c)) {
                        set = line2Set;
                    } else if (line3Set.contains(c)) {
                        set = line3Set;
                    }
                }
                if (!set.contains(c)) {
                    lineOne = false;
                    break;
                }
            }
            if (lineOne) {
                res.add(word);
            }
        }

        String[] as = new String[res.size()];
        for (int i = 0; i < res.size(); i++) {
            as[i] = res.get(i);
        }
        return as;
    }
    */

    static Map<Character, Integer> map = new HashMap<>(26);

    static {
        for (char c : "qwertyuiop".toCharArray()) {
            map.put(c, 1);
        }
        for (char c : "asdfghjkl".toCharArray()) {
            map.put(c, 2);
        }
        for (char c : "zxcvbnm".toCharArray()) {
            map.put(c, 3);
        }
    }

    public String[] findWords(String[] words) {
        List<String> res = new ArrayList<>();
        for (String word : words) {
            char[] chars = word.toCharArray();
            boolean lineOne = true;
            int line = 0;
            for (char aChar : chars) {
                char c = Character.toLowerCase(aChar);
                if (line == 0) {
                    line = map.getOrDefault(c, 0);
                }
                if (line != map.get(c)) {
                    lineOne = false;
                    break;
                }
            }
            if (lineOne) {
                res.add(word);
            }
        }

        String[] as = new String[res.size()];
        for (int i = 0; i < res.size(); i++) {
            as[i] = res.get(i);
        }
        return as;
    }


    @Test
    public void findWordsTest() {
        Assert.assertArrayEquals(new String[]{"Alaska", "Dad"}, findWords(new String[]{"Hello", "Alaska", "Dad", "Peace"}));
        Assert.assertArrayEquals(new String[]{}, findWords(new String[]{"omk"}));
        Assert.assertArrayEquals(new String[]{"adsdf", "sfd"}, findWords(new String[]{"adsdf", "sfd"}));
    }

    @Test
    public void findWordsTest222() {
       // String url = "https://lihcengtestzone.oss-cn-zhangjiakou.aliyuncs.com/bulktrade/uat/2023/01/13/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20221202171706.png/290868fd-78cc-4a03-9135-6160c3a3e04b%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20221202171706.png?Expires=1673601324&OSSAccessKeyId=LTAI5tCH8qDa6HUd7F9nd5W7&Signature=waG4kek29BJbSNpjkHudSqkTigY%3D";
       String url = "https://www.crunchydata.com/blog/pgbackrest-performing-backups-on-a-standby-cluster";
        try {
            System.out.println(URLDecoder.decode(url, "utf-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
